<?php
namespace app\index\model;

use app\common\model\RemoteModel;
use think\Db;
use think\Log;

class DCharacter extends RemoteModel
{
    //==============
    // 定义及初始化
    //==============

    //等级区间
    protected $level_min = 1;
    protected $level_max = 10;

    //计入非流失天数
    //
    protected $active_days_scope = 7;

    protected $connection = 'mysql://root:JB6MUn64xICo@rm-bp1h205f36fv8pecto.mysql.rds.aliyuncs.com:3306/dragon#utf8';
    protected $url        = "http://115.239.196.59:19998/?sid=144169&mid=20005&serverid=1&cid=1&tablename=d_character";

    // 关闭自动写入update_time字段

    protected $updateTime = false;

    //重写父类initialize()

    protected function initialize()
    {
        parent::initialize();
        $this->table = 'd_character';
    }

    //==============
    // 数据模型-方法
    //==============

    /**
     * 全量插入最新数据
     *
     * @return int 插入数据量
     */
    public function updateToRecent()
    {
        $this->clearTbl();
        $this->getAllRecords();
    }

    /**
     * 禁言状态修改
     *
    public static function changeDontalkTo($id, $v)
    {

        try {
            self::update(['id' => $id, 'donttalk' => $v]);
        } catch (\Exception $e) {
            throw new \think\Exception('异常消息:' . $e->getMessage());
        }
    }*/

    /**
     * 等级分布列表
     *
     * 根据等级分布范围计算等级分布及流失
     *
     * @return   void
     */
    public function doList()
    {
        $res = $this->doLevelScopeCompute($this->level_min, $this->level_max);
        return $res;
    }

    /**
     * 等级分布流失计算
     *
     * @return   [type]
     */
    public function doLevelScopeCompute($level_min, $level_max)
    {

        //计算结果
        $rtn = [];

        //逐级计算
        //
        for ($level = $level_min; $level <= $level_max; $level++) {

            $row = [];
            //分项计算
            //
            $row['channel_id'] = "IOS";
            $row['server']     = 1;
            $row['level']      = $level;
            $row['total']      = $this->doLevelCount($level);
            $row['active']     = $this->doLevelCountActive($level, $this->active_days_scope);
            $row['inactive']   = $row['total'] - $row['active'];

            if ($this->doLevelCount($level) > 0) {

                $row['inactive_rate'] = bcdiv(($row['total'] - $row['active']), $row['total'], 3);

            } else {

                $row['inactive_rate'] = null;
            }

            array_push($rtn, $row);
        }

        $res['total'] = count($rtn);
        $res['rows']  = $rtn;

        return $res;

    }

    /**
     * 等级人数计算
     * @DateTime 2018-01-04
     * @param    int $level 等级
     * @return   int 等级分布人数
     */
    public function doLevelCount($level)
    {
        $map['level'] = $level;
        $count        = $this->where($map)->count();

        Log::info(self::getLastSql());
        return $count;
    }

    /**
     * 等级活跃角色ID计算
     * 
     * @param    int $level 等级
     * @return   int 等级流失人数
     */
    public function doLevelCountActive($level, $active_days_scope)
    {

        $res   = Db::query("SELECT COUNT(DISTINCT `id`) AS `count` FROM `d_character` WHERE DATEDIFF(now(),`lastonlogintime`) <= {$active_days_scope} AND `level`={$level} LIMIT 1");
        $count = $res[0]['count'];

        Log::info(self::getLastSql());
        return $count;

    }
    /**
     * 流失率
     * @DateTime 2018-01-04
     * @param    [type] $value [description]
     * @param    [type] $data [description]
     * @return   [type]
     */
    public function getinactiveRateattr($value, $data)
    {
        $Inactive_rate = bcdiv(($data['total'] - $data['active']), $data['total'], 3);

        return $Inactive_rate;
    }
}
